package com.example.pathprotectsystem.controller;

import com.example.pathprotectsystem.pojo.AskList;
import com.example.pathprotectsystem.pojo.PageBean;
import com.example.pathprotectsystem.pojo.Path;
import com.example.pathprotectsystem.pojo.Result;
import com.example.pathprotectsystem.service.BindService;
import com.example.pathprotectsystem.service.PathService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class PathController {
    @Autowired
    private PathService pathService;
    @Autowired
    private BindService bindService;
    @PostMapping("/addpath")
    public Result addpath(@RequestBody Path path){
        String name = path.getPathName();
        int num = pathService.findPathByPathName(name);
        if(num==0){
            pathService.addpath(path);
            return Result.success();
        }
        return Result.error("道路名称已存在，无法添加！");
    }

    @PostMapping("/pathlist")
    public Result<PageBean<Path>> pathlist(@RequestBody AskList askList){
        PageBean<Path> p = pathService.pathlist(askList);
        return Result.success(p);
    }
    @GetMapping("/pathnameall")
    public Result<List<String>> pathall(){
        List<String> p = pathService.pathall();
        return Result.success(p);
    }
    @GetMapping("/getallpath")
    public Result<List<Path>> getallpath(){
        List<Path> p = pathService.getallpath();
        return Result.success(p);
    }

    @PutMapping("/pathupdate")
    public Result pathupdate(@RequestBody Path path){
        String name = path.getPathName();
        int num = pathService.findPathByPathName(name);
        pathService.pathupdate(path);
        return Result.success();
    }

    @DeleteMapping("/pathdelete")
    public Result pathdelete(int id){
        int sum = bindService.findPathIdFromTask(id);
        System.out.println("sum = "+sum);
        if(sum==0){
            pathService.pathdelete(id);
            return Result.success();
        }
        return Result.error("有巡查或检查任务存在该条道路信息，无法删除！");
    }
}
